// 引入coolsite360交互配置设定
require('coolsite.config.js');

import {
  roleDict
} from '../../../../config';
import {
  getIdShowTextFromUserInfo,
  injectUserInfoFromStorageIntoData,
} from '../../../../utils/common';

// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称
   */
  name: "five",
  /**
   * 页面的初始数据
   */

  data: {

    successInfoBgColor: app.globalData.successInfoBgColor,
    failInfoBgColor: app.globalData.failInfoBgColor,
    warmBgColor: app.globalData.warmBgColor,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  init: function () {},
  onLoad() {
    this.init();
    // 注册coolsite360交互模块
    app.coolsite360.register(this);
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.warmBgColor,
    });
    // injectUserInfoFromStorageIntoData.apple(this, []);
    app.editTabBarTeacher2();

    // // var role = wx.getStorageSync('role');
    // const role = this.data.userInfo.role;
    // if (role == '1')
    //   app.editTabBarTeacher1();
    // else app.editTabBarTeacher2();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 检测storage中有没有wxopenid,没有就转跳到授权页面
    // app.wxUserManager.checkWxopenidInStorageAtMypage();
    const wxopenid = wx.getStorageSync('wxopenid')
    const userInfoDict = wx.getStorageSync('userInfoDict')
    if (!wxopenid || !(wxopenid in userInfoDict)) {
      wx.redirectTo({
        url: '/page/userForm/userForm',
      })
      return;
    }
    const userInfo = userInfoDict[wxopenid];
    const idShowText = getIdShowTextFromUserInfo.apply(this, [userInfo]);
    this.setData({
      userInfo: userInfo,
      roleText: roleDict[userInfo.role].text,
      idShowText,
    })
    // var name = wx.getStorageSync('name');
    // var role=wx.getStorageSync('role');
    // var teacher_id = wx.getStorageSync('teacher_id');
    // var school=wx.getStorageSync('school');
    // this.setData({
    //   name: name,
    //   teacher_id:teacher_id,
    //   role:role,
    //   school:school
    // })

    // 执行coolsite360交互组件展示
    app.coolsite360.onShow(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },


  storageLogout() {
    const wxopenid = wx.getStorageSync('wxopenid');
    const userInfoDict = wx.getStorageSync('userInfoDict');
    delete userInfoDict[wxopenid]
    wx.setStorageSync('wxopenid', '')
    wx.setStorageSync('userInfoDict', userInfoDict)
    wx.redirectTo({
      url: '/page/index/index',
    })
  },
  //以下为自定义点击事件
  goUserForm() {
    wx.navigateTo({
      url: '/page/userForm/userForm',
    })
  },
  goChangePwd: function () {
    wx.navigateTo({
      url: '../change/changepwd',
    })
  },
  cancelConfirmExit: function () {
    wx.showModal({
      title: '提示',
      content: '确定要退出吗',
      success: function (res) {
        if (res.confirm) app.exit();
      }
    })
  },
})